package com.szxy.common.redis.service.impl;

import com.szxy.common.redis.service.RedisCartService;
import com.szxy.utils.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Auther:zwer
 * @Date:2019/9/25 15:35
 * @Description:com.szxy.common.redis.service.impl
 * @Version:1.0
 **/
@Service
public class RedisCartServiceImpl implements RedisCartService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Value("${FRONT_CART_REDIS_KEY}")
    private String FRONT_CART_REDIS_KEY;

    @Override
    public void insertToCart(Map<String, Object> map) {
        String userId = (String) map.get("userId");
        Map<String, CartItem> cartMap = (Map<String, CartItem>) map.get("cart");
        this.redisTemplate.opsForHash().put(this.FRONT_CART_REDIS_KEY
        , userId, cartMap );
    }

    @Override
    public Map<String, CartItem> selectCartByUserId(String userId) {
        return (Map<String, CartItem>) this.redisTemplate.opsForHash().get(this.FRONT_CART_REDIS_KEY
        , userId);
    }
}
